package main.social.service.impl;

import java.util.List;

import main.social.service.AccountService;
import main.social.service.util.Utils;
import main.social.dao.AccountDao;
import main.social.entities.Accounts;

public class AccountServiceImpl implements AccountService {
    AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    /**
     * findMyObjectById
     *
     * @param id int
     * @return MyObjectVO
     * @todo Implement this org.annotationmvc.service.MyObjectService method
     */
    public Accounts findAccountById(int id) {
        return accountDao.findAccountById(id);
    }

    /**
     * getAllMyObjectVOs
     *
     * @return List
     * @todo Implement this org.annotationmvc.service.MyObjectService method
     */
    @SuppressWarnings("rawtypes")
	public List getAllAccounts() {
        return accountDao.findAllAccounts();
    }

    /**
     * insertMyObjectVO
     *
     * @param myObjectVO MyObjectVO
     * @todo Implement this org.annotationmvc.service.MyObjectService method
     */
    public void insertAccount(Accounts myObjectVO) {
        accountDao.insertAccount(myObjectVO);
    }

    /**
     * removeMyObjectVO
     *
     * @param myObjectVO MyObjectVO
     * @todo Implement this org.annotationmvc.service.MyObjectService method
     */
    public void removeAccount(Accounts myObjectVO) {
        accountDao.removeAccount(myObjectVO);
    }

    /**
     * updateMyObjectVO
     *
     * @param myObjectVO MyObjectVO
     * @todo Implement this org.annotationmvc.service.MyObjectService method
     */
    public void updateAccount(Accounts myObjectVO) {
       accountDao.updateAccount(myObjectVO);
    }

	@Override
	public Accounts findAccountById(long id) {
		return accountDao.findAccountById(id);
	}

	@Override
	public Code insertAccount(String username, String password, String email) {
		Accounts newAccount = null;
		newAccount = this.accountDao.findAccountByUsername(username);
		if(newAccount != null){
			return AccountService.Code.CONFLICT_USERNAME;
		}
		newAccount = this.accountDao.findAccountByEmail(email);
		if(newAccount != null){
			return AccountService.Code.CONFLICT_EMAIL;
		}
		newAccount = this.accountDao.newEntityInstance();
		newAccount.setUsername(username);
		newAccount.setPassword(password);
		newAccount.setEmail(email);
		newAccount.setActivecode(Utils.createUUIDCode(20));
		if(this.accountDao.insertAccount(newAccount)){
			return AccountService.Code.UNHANDLE_ERROR;
		}
		Utils.SendMail(email, "Active your account", newAccount.getActivecode());
		return AccountService.Code.SUCCESS;
	}

	@Override
	public boolean Login(String username, String password) {
		return this.accountDao.Login(username, password);
	}
}
